package com.fingard.dsp.bank.directbank.hzcb01;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillTrans;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.*;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.detail.*;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.sql.Connection;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author Parker on 2020-04-03.
 * @version 1.0
 */
public class HZCB01ElecBillTrans extends HZCB01Base implements IElecBillTrans {

    //通用签收
    @Override
    public RetAtsB003BillRespSubmit D_B003_BillResp_Submit(ReqB003BillRespSubmit query) throws Exception {
        RetAtsB003BillRespSubmit retAts = new RetAtsB003BillRespSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        Connection conn = FGBiz.getLocalDbConn();
        try {
            while (query.readRecord()) {//赋值通用参数
                RetAtsB003BillRespSubmitDetail detail = new RetAtsB003BillRespSubmitDetail();
                txCode = "PY4035";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("input");
                xmlWriter.writeElementString("compFlowNo", query.detail.reqSeqID);//企业流水号
                xmlWriter.writeElementString("totalNum", "1");//总笔数
                xmlWriter.writeStartElement("result");
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("applicantAcctNo", query.detail.actNum);//客户账号
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (map.size() == 0) {
                    detail.respCode = "-1";
                    detail.respInfo = "没有找到票据号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("billId", map.get("BANKREQSEQID").toString());//票据ID
                xmlWriter.writeElementString("signUpMark", "1".equals(query.detail.respFlag) ? "1" : "0");//签收意见1 同意 0 拒绝
                String bkType;
                switch (Integer.valueOf(query.detail.respType)) {
                    //1-承兑应答 2-收票应答 3-背书转让应答 4-回购式贴现赎回应答 5-质押解除应答 6-提示付款应答 7-保证应答 8-同意清偿应 9-质押签收
                    case 1:
                        bkType = "200202";
                        break;
                    case 2:
                        bkType = "200302";
                        break;
                    case 3:
                        bkType = "201002";
                        break;
                    case 4:
                        bkType = "201202";
                        break;
                    case 5:
                        bkType = "201902";
                        break;
                    case 6:
                        bkType = "202002";
                        break;
                    case 7:
                        bkType = "201702";
                        break;
                    case 8:
                        throw new Exception("银行暂不支持保证类型的签收");
                    case 9:
                        bkType = "201802";
                        break;
                    default:
                        throw new Exception("未知的签收类型：" + query.detail.respType);
                }
                xmlWriter.writeElementString("transNo", bkType);// 业务类型
                //拒付理由
                if ("6".equals(query.detail.respType) && "2".equals(query.detail.respFlag)) {
                    if (StringHelper.isNullOrEmpty(query.detail.unPayCode)) {
                        throw new Exception("提示付款拒签时，拒付理由不能为空!");
                    }
                    if ("99".equals(query.detail.unPayCode)) {
                        xmlWriter.writeElementString("remark1", "DC09");
                    } else {
                        xmlWriter.writeElementString("remark1", "DC0" + (Integer.valueOf(query.detail.unPayCode) - 1));
                    }
                } else {
                    xmlWriter.writeElementString("remark1", "");
                }
                xmlWriter.writeElementString("remark1", query.detail.reqSeqID);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode);
                    detail.respCode = commonSubmitDetaildetail.respCode;
                    detail.respInfo = commonSubmitDetaildetail.respInfo;
                    detail.transState = commonSubmitDetaildetail.transState;
                    detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                    detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSign[1];
                }
                retAts.writeDetail(detail);
            }
        } catch (Exception e) {
            RetAtsB003BillRespSubmitDetail detail = new RetAtsB003BillRespSubmitDetail();
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            retAts.writeDetail(detail);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    //出票
    @Override
    public RetAtsB101BillApplySubmit D_B101_BillResp_Submit(ReqB101BillApplySubmit query) throws Exception {
        RetAtsB101BillApplySubmit retAts = new RetAtsB101BillApplySubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        while (query.readRecord()) {//赋值通用参数
            RetAtsB101BillApplySubmitDetail detail = new RetAtsB101BillApplySubmitDetail();
            txCode = "PY4011";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("input");
            xmlWriter.writeElementString("compFlowNo", query.detail.reqSeqID);//企业流水号
            xmlWriter.writeElementString("totalNum", "1");//总笔数
            xmlWriter.writeStartElement("result");
            xmlWriter.writeStartElement("row");
            xmlWriter.writeElementString("applicantAcctNo", query.detail.drawAct);//申请人账号
            xmlWriter.writeElementString("source", "");//发起来源
            xmlWriter.writeElementString("flag", "2");// 处理标识 0:退回 1：删除 2：出票登记
            xmlWriter.writeElementString("billType", query.detail.billType);// 票据类型
            xmlWriter.writeElementString("billClass", "2");// 票据种类
            xmlWriter.writeElementString("acptDt", DateHelper.convertDateTimetoD8(query.detail.issDate));// 出票日期
            xmlWriter.writeElementString("dueDt", DateHelper.convertDateTimetoD8(query.detail.dueDate));// 汇票到期日
            xmlWriter.writeElementString("billMoney", query.detail.billAmt);//票据金额
            xmlWriter.writeElementString("remitterCustId", getBankConfig().getMapValueByKey("orgCodeMapping", query.detail.drawCode));//出票人客户号
            xmlWriter.writeElementString("remitter", query.detail.drawName);//出票人全称
            xmlWriter.writeElementString("remitterAcctNo", query.detail.drawAct);//出票人账号
            xmlWriter.writeElementString("remitterBankName", query.detail.drawBnkName);//出票人开户行
            xmlWriter.writeElementString("remitterBankNo", query.detail.drawBnkCode);//出票人开户行行号
            xmlWriter.writeElementString("payeeCustId", "");//收款人客户号
            xmlWriter.writeElementString("payee", query.detail.payeeName);//收款人全称
            xmlWriter.writeElementString("payeeAcctNo", query.detail.payeeAct);//收款人账号
            xmlWriter.writeElementString("payeeBankName", query.detail.payeeBnkName);//收款人开户行行名
            xmlWriter.writeElementString("payeeBankNo", query.detail.payeeBnkCode);//收款人开户行行号
            xmlWriter.writeElementString("acceptor", query.detail.accpName);//承兑人全称
            if ("1".equalsIgnoreCase(query.detail.billType)) {
                xmlWriter.writeElementString("acceptorAcctNo", "0");//承兑人账号
            } else {
                xmlWriter.writeElementString("acceptorAcctNo", query.detail.accpAct);//承兑人账号
            }
            xmlWriter.writeElementString("acceptorBankName", query.detail.accpBnkName);//承兑人开户行行名
            xmlWriter.writeElementString("acceptorBankNo", query.detail.accpBnkCode);//承兑人开户行行号
            xmlWriter.writeElementString("forbidFlag", "1".equals(query.detail.transFlag) ? "0" : "1");//禁止背书标记
            xmlWriter.writeElementString("remitterRemark", query.detail.reqSeqID);//出票人备注
            xmlWriter.writeElementString("invoiceNo", query.detail.invoiceNo);//发票号
            xmlWriter.writeElementString("reserve1", query.detail.memo);//预留字段
            xmlWriter.writeElementString("conferNo", query.detail.contractNo);//合同号
            xmlWriter.writeElementString("payeeType", "1".equals(query.detail.payeeSameBnk) ? "1" : "0");//收票人是否本行
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            String signXml = getDataSignData(xmlWriter);
            if (signnatrue(signXml) == true) {
                xmlWriter.writeElementString("signData", signature);
                xmlWriter.writeEndDocument();//写入数据完成
                String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode,txCode);
                detail.respCode = commonSubmitDetaildetail.respCode;
                detail.respInfo = commonSubmitDetaildetail.respInfo;
                detail.transState = commonSubmitDetaildetail.transState;
                detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpSign[1];
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }


    //提示承兑
    @Override
    public RetAtsB102BillAccpSubmit D_B102_BillResp_Submit(ReqB102BillAccpSubmit query) throws Exception {
        RetAtsB102BillAccpSubmit retAts = new RetAtsB102BillAccpSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        Connection conn = FGBiz.getLocalDbConn();
        try {
            while (query.readRecord()) {
                RetAtsB102BillAccpSubmitDetail detail = new RetAtsB102BillAccpSubmitDetail();
                txCode = "PY4014";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("input");
                xmlWriter.writeElementString("compFlowNo", query.detail.reqSeqID);//企业流水号
                xmlWriter.writeElementString("totalNum", "1");//总笔数
                xmlWriter.writeStartElement("result");
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("applicantAcctNo", query.detail.actNum);//客户账号
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (map.size() == 0) {
                    detail.respCode = "-1";
                    detail.respInfo = "没有找到票据号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("billId", map.get("BANKREQSEQID").toString());//票据ID
                xmlWriter.writeElementString("source", "");//发起来源
                xmlWriter.writeElementString("remark", query.detail.reqSeqID);//备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode);
                    detail.respCode = commonSubmitDetaildetail.respCode;
                    detail.respInfo = commonSubmitDetaildetail.respInfo;
                    detail.transState = commonSubmitDetaildetail.transState;
                    detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                    detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSign[1];
                }
                retAts.writeDetail(detail);
            }
        } catch (Exception e) {
            RetAtsB102BillAccpSubmitDetail detail = new RetAtsB102BillAccpSubmitDetail();
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            retAts.writeDetail(detail);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    //撤票
    @Override
    public RetAtsB103BillRetSubmit D_B103_BillResp_Submit(ReqB103BillRetSubmit query) throws Exception {
        RetAtsB103BillRetSubmit retAts = new RetAtsB103BillRetSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        Connection conn = FGBiz.getLocalDbConn();
        try {
            while (query.readRecord()) {
                RetAtsB103BillRetSubmitDetail detail = new RetAtsB103BillRetSubmitDetail();
                txCode = "PY4013";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("input");
                xmlWriter.writeElementString("compFlowNo", query.detail.reqSeqID);//企业流水号
                xmlWriter.writeElementString("totalNum", "1");//总笔数
                xmlWriter.writeStartElement("result");
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("applicantAcctNo", query.detail.actNum);//客户账号
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (map.size() == 0) {
                    detail.respCode = "-1";
                    detail.respInfo = "没有找到票据号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("billId", map.get("BANKREQSEQID").toString());//票据ID
                xmlWriter.writeElementString("source", "");//发起来源
                xmlWriter.writeElementString("remark", query.detail.reqSeqID);//备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode);
                    detail.respCode = commonSubmitDetaildetail.respCode;
                    detail.respInfo = commonSubmitDetaildetail.respInfo;
                    detail.transState = commonSubmitDetaildetail.transState;
                    detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                    detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSign[1];
                }
                retAts.writeDetail(detail);
            }
        } catch (Exception e) {
            RetAtsB103BillRetSubmitDetail detail = new RetAtsB103BillRetSubmitDetail();
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            retAts.writeDetail(detail);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    //提示收票
    @Override
    public RetAtsB104BillReceSubmit D_B104_BillResp_Submit(ReqB104BillReceSubmit query) throws Exception {
        RetAtsB104BillReceSubmit retAts = new RetAtsB104BillReceSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        Connection conn = FGBiz.getLocalDbConn();
        try {
            while (query.readRecord()) {
                RetAtsB104BillReceSubmitDetail detail = new RetAtsB104BillReceSubmitDetail();
                txCode = "PY4012";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("input");
                xmlWriter.writeElementString("compFlowNo", query.detail.reqSeqID);//企业流水号
                xmlWriter.writeElementString("totalNum", "1");//总笔数
                xmlWriter.writeStartElement("result");
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("applicantAcctNo", query.detail.actNum);//客户账号
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (map.size() == 0) {
                    detail.respCode = "-1";
                    detail.respInfo = "没有找到票据号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("billId", map.get("BANKREQSEQID").toString());//票据ID
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode);
                    detail.respCode = commonSubmitDetaildetail.respCode;
                    detail.respInfo = commonSubmitDetaildetail.respInfo;
                    detail.transState = commonSubmitDetaildetail.transState;
                    detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                    detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSign[1];
                }
                retAts.writeDetail(detail);
            }
        } catch (Exception e) {
            RetAtsB104BillReceSubmitDetail detail = new RetAtsB104BillReceSubmitDetail();
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            retAts.writeDetail(detail);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    //背书转让申请
    @Override
    public RetAtsB105BillEndoSubmit D_B105_BillResp_Submit(ReqB105BillEndoSubmit query) throws Exception {
        RetAtsB105BillEndoSubmit retAts = new RetAtsB105BillEndoSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        Connection conn = FGBiz.getLocalDbConn();
        try {
            while (query.readRecord()) {
                RetAtsB105BillEndoSubmitDetail detail = new RetAtsB105BillEndoSubmitDetail();
                txCode = "PY4015";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("input");
                xmlWriter.writeElementString("compFlowNo", query.detail.reqSeqID);//企业流水号
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (map.size() == 0) {
                    detail.respCode = "-1";
                    detail.respInfo = "没有找到票据号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("billId", map.get("BANKREQSEQID").toString());//票据ID
                xmlWriter.writeElementString("billNo", query.detail.billNo);//票据号
                xmlWriter.writeElementString("receiverName", query.detail.receName);//被背书人名称
                xmlWriter.writeElementString("receiverAcctNo", query.detail.receAct);//被背书人帐号
                xmlWriter.writeElementString("receiverBankNo", query.detail.receBnkCode);//被背书人开户行号
                xmlWriter.writeElementString("forbidFlag", "1".equals(query.detail.transFlag) ? "0" : "1");//禁止背书标记
                xmlWriter.writeElementString("applicantAcctNo", query.detail.actNum);//背书账号
                xmlWriter.writeElementString("remark", query.detail.reqSeqID);//背书人备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode);
                    detail.respCode = commonSubmitDetaildetail.respCode;
                    detail.respInfo = commonSubmitDetaildetail.respInfo;
                    detail.transState = commonSubmitDetaildetail.transState;
                    detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                    detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSign[1];
                }
                retAts.writeDetail(detail);
            }
        } catch (Exception e) {
            RetAtsB105BillEndoSubmitDetail detail = new RetAtsB105BillEndoSubmitDetail();
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            retAts.writeDetail(detail);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    //贴现申请
    @Override
    public RetAtsB106BillDiscSubmit D_B106_BillResp_Submit(ReqB106BillDiscSubmit query) throws Exception {
        RetAtsB106BillDiscSubmit retAts = new RetAtsB106BillDiscSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        Connection conn = FGBiz.getLocalDbConn();
        try {
            while (query.readRecord()) {
                RetAtsB106BillDiscSubmitDetail detail = new RetAtsB106BillDiscSubmitDetail();
                txCode = "PY4019";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("input");
                xmlWriter.writeElementString("compFlowNo", query.detail.reqSeqID);//企业流水号
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (map.size() == 0) {
                    detail.respCode = "-1";
                    detail.respInfo = "没有找到票据号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("billId", map.get("BANKREQSEQID").toString());//票据ID
                xmlWriter.writeElementString("applicantAcctNo", query.detail.actNum);//客户帐号
                xmlWriter.writeElementString("discType", "1".equals(query.detail.discType) ? "0" : "1");//贴现方式0:买断式1赎回式
                xmlWriter.writeElementString("rate", query.detail.discRate);//贴现利率
                xmlWriter.writeElementString("discDt", DateHelper.convertDateTimetoD8(query.detail.discDate));//贴现日期
                xmlWriter.writeElementString("redeemOpenDt", DateHelper.convertDateTimetoD8(query.detail.repOpenDate));//赎回开放日
                xmlWriter.writeElementString("redeemEndDt", DateHelper.convertDateTimetoD8(query.detail.repDueDate));//赎回截止日
                xmlWriter.writeElementString("redeemRate", query.detail.repRate);//赎回利率
                xmlWriter.writeElementString("toBankName", query.detail.receBnkName);//贴入行
                xmlWriter.writeElementString("toBankNo", query.detail.receBnkCode);//贴入行行号
                xmlWriter.writeElementString("toAcctNo", query.detail.receAct);//贴入行账号
                xmlWriter.writeElementString("inAcctNo", query.detail.actedAct);//入账帐号
                xmlWriter.writeElementString("inAcctBankNo", query.detail.actedBnkCode);//入账行号
                xmlWriter.writeElementString("batchNo", query.detail.reqSeqID);//批次号
                xmlWriter.writeElementString("forbidFlag", "1".equals(query.detail.transFlag) ? "0" : "1");//可背书标记 0：可背书 1：不可背书
                xmlWriter.writeElementString("onlineMark", "2".equals(query.detail.payType) ? "0" : "1");//线上清算标记0-线下    1-线上
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode);
                    detail.respCode = commonSubmitDetaildetail.respCode;
                    detail.respInfo = commonSubmitDetaildetail.respInfo;
                    detail.transState = commonSubmitDetaildetail.transState;
                    detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                    detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSign[1];
                }
                retAts.writeDetail(detail);
            }
        } catch (Exception e) {
            RetAtsB106BillDiscSubmitDetail detail = new RetAtsB106BillDiscSubmitDetail();
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            retAts.writeDetail(detail);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    //质押申请
    @Override
    public RetAtsB107BillPledSubmit D_B107_BillResp_Submit(ReqB107BillPledSubmit query) throws Exception {
        RetAtsB107BillPledSubmit retAts = new RetAtsB107BillPledSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        Connection conn = FGBiz.getLocalDbConn();
        try {
            while (query.readRecord()) {
                RetAtsB107BillPledSubmitDetail detail = new RetAtsB107BillPledSubmitDetail();
                txCode = "PY4016";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("input");
                xmlWriter.writeElementString("compFlowNo", query.detail.reqSeqID);//企业流水号
                xmlWriter.writeElementString("totalNum", "1");//总笔数
                xmlWriter.writeStartElement("result");
                xmlWriter.writeStartElement("row");
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (map.size() == 0) {
                    detail.respCode = "-1";
                    detail.respInfo = "没有找到票据号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("billId", map.get("BANKREQSEQID").toString());//票据ID
                xmlWriter.writeElementString("applicantAcctNo", query.detail.actNum);//客户账号
                xmlWriter.writeElementString("receiverName", query.detail.receName);//质权人名称
                xmlWriter.writeElementString("receiverAcctNo", query.detail.receAct);//质权人账号
                xmlWriter.writeElementString("receiverBankNo", query.detail.receBnkCode);//质权人开户行行号
                xmlWriter.writeElementString("batchNo", "");//批次号
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();

                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode);
                    detail.respCode = commonSubmitDetaildetail.respCode;
                    detail.respInfo = commonSubmitDetaildetail.respInfo;
                    detail.transState = commonSubmitDetaildetail.transState;
                    detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                    detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSign[1];
                }
                retAts.writeDetail(detail);
            }
        } catch (Exception e) {
            RetAtsB107BillPledSubmitDetail detail = new RetAtsB107BillPledSubmitDetail();
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            retAts.writeDetail(detail);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    //提示付款
    @Override
    public RetAtsB108BillPaySubmit D_B108_BillResp_Submit(ReqB108BillPaySubmit query) throws Exception {
        RetAtsB108BillPaySubmit retAts = new RetAtsB108BillPaySubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        Connection conn = FGBiz.getLocalDbConn();
        try {
            while (query.readRecord()) {
                RetAtsB108BillPaySubmitDetail detail = new RetAtsB108BillPaySubmitDetail();
                txCode = "PY4018";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("input");
                xmlWriter.writeElementString("compFlowNo", query.detail.reqSeqID);//企业流水号
                xmlWriter.writeElementString("totalNum", "1");//总笔数
                xmlWriter.writeStartElement("result");
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("applicantAcctNo", query.detail.actNum);//客户账号
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (map.size() == 0) {
                    detail.respCode = "-1";
                    detail.respInfo = "没有找到票据号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("billId", map.get("BANKREQSEQID").toString());//票据ID
                xmlWriter.writeElementString("applyDt", Format.Date8Format.format(new Date()));//提示付款申请日期
                xmlWriter.writeElementString("overdueReason", query.detail.overDueRea);//逾期原因说明
                xmlWriter.writeElementString("remark", query.detail.reqSeqID);//提示付款备注
                xmlWriter.writeElementString("onlineMark", "2".equals(query.detail.payType) ? "0" : "1");//线上清算标记
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();

                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode);
                    detail.respCode = commonSubmitDetaildetail.respCode;
                    detail.respInfo = commonSubmitDetaildetail.respInfo;
                    detail.transState = commonSubmitDetaildetail.transState;
                    detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                    detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSign[1];
                }
                retAts.writeDetail(detail);
            }
        } catch (Exception e) {
            RetAtsB108BillPaySubmitDetail detail = new RetAtsB108BillPaySubmitDetail();
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            retAts.writeDetail(detail);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    //保证申请
    @Override
    public RetAtsB109BillGuarSubmit D_B109_BillResp_Submit(ReqB109BillGuarSubmit query) throws Exception {
        RetAtsB109BillGuarSubmit retAts = new RetAtsB109BillGuarSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB109BillGuarSubmitDetail detail = new RetAtsB109BillGuarSubmitDetail();
        detail.respCode = "-1";
        detail.respInfo = "银行暂不支持此操作";
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    //追索通知
    @Override
    public RetAtsB110BillRecoSubmit D_B110_BillResp_Submit(ReqB110BillRecoSubmit query) throws Exception {
        RetAtsB110BillRecoSubmit retAts = new RetAtsB110BillRecoSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB110BillRecoSubmitDetail detail = new RetAtsB110BillRecoSubmitDetail();
        detail.respCode = "-1";
        detail.respInfo = "银行暂不支持此操作";
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    //同意清偿
    @Override
    public RetAtsB111BillAgreeSubmit D_B111_BillResp_Submit(ReqB111BillAgreeSubmit query) throws Exception {
        RetAtsB111BillAgreeSubmit retAts = new RetAtsB111BillAgreeSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB111BillAgreeSubmitDetail detail = new RetAtsB111BillAgreeSubmitDetail();
        detail.respCode = "-1";
        detail.respInfo = "银行暂不支持此操作";
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    //交易撤回
    @Override
    public RetAtsB112BillCancSubmit D_B112_BillResp_Submit(ReqB112BillCancSubmit query) throws Exception {
        RetAtsB112BillCancSubmit retAts = new RetAtsB112BillCancSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        Connection conn = FGBiz.getLocalDbConn();
        try {
            while (query.readRecord()) {
                RetAtsB112BillCancSubmitDetail detail = new RetAtsB112BillCancSubmitDetail();
                txCode = "PY4034";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("input");
                xmlWriter.writeElementString("compFlowNo", query.reqHeader.transSeqID);//企业流水号
                xmlWriter.writeElementString("totalNum", "1");//总笔数
                xmlWriter.writeStartElement("result");
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("applicantAcctNo", query.detail.actNum);//客户账号
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (map.size() == 0) {
                    detail.respCode = "-1";
                    detail.respInfo = "没有找到票据号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("billId", map.get("BANKREQSEQID").toString());//票据ID
                xmlWriter.writeElementString("source", "");//发起来源
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();

                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode);
                    detail.respCode = commonSubmitDetaildetail.respCode;
                    detail.respInfo = commonSubmitDetaildetail.respInfo;
                    detail.transState = commonSubmitDetaildetail.transState;
                    detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                    detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSign[1];
                }
                retAts.writeDetail(detail);
            }
        } catch (Exception e) {
            RetAtsB112BillCancSubmitDetail detail = new RetAtsB112BillCancSubmitDetail();
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            retAts.writeDetail(detail);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    //协议出票
    @Override
    public RetAtsB113ProtocolSubmit D_B113_ProtocolResp_Submit(ReqB113ProtocolSubmit query) throws Exception {
        RetAtsB113ProtocolSubmit retAts = new RetAtsB113ProtocolSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        RetAtsB113ProtocolSubmitDetail detail = new RetAtsB113ProtocolSubmitDetail();
        detail.respCode = "-1";
        detail.respInfo = "银行暂不支持此操作";
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    //质押解除
    @Override
    public RetAtsB119BillRelePledSubmit D_B119_BillResp_Submit(ReqB119BillRelePledSubmit query) throws Exception {
        RetAtsB119BillRelePledSubmit retAts = new RetAtsB119BillRelePledSubmit(query.reqHeader);
        retAts.writeBeforeDetail();
        Connection conn = FGBiz.getLocalDbConn();
        try {
            while (query.readRecord()) {
                RetAtsB119BillRelePledSubmitDetail detail = new RetAtsB119BillRelePledSubmitDetail();
                txCode = "PY4017";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("input");
                xmlWriter.writeElementString("compFlowNo", query.detail.reqSeqID);//企业流水号
                xmlWriter.writeElementString("totalNum", "1");//总笔数
                xmlWriter.writeStartElement("result");
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("applicantAcctNo", query.detail.actNum);//客户账号
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (map.size() == 0) {
                    detail.respCode = "-1";
                    detail.respInfo = "没有找到票据号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("billId", map.get("BANKREQSEQID").toString());//票据ID
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();

                String signXml = getDataSignData(xmlWriter);
                if (signnatrue(signXml) == true) {
                    xmlWriter.writeElementString("signData", signature);
                    xmlWriter.writeEndDocument();//写入数据完成
                    String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                    RetAtsCommonSubmitDetail commonSubmitDetaildetail = getRspResultCode(tmpStrRet, query.detail.reqSeqID, query.reqHeader.bankCode);
                    detail.respCode = commonSubmitDetaildetail.respCode;
                    detail.respInfo = commonSubmitDetaildetail.respInfo;
                    detail.transState = commonSubmitDetaildetail.transState;
                    detail.payInfoCode = commonSubmitDetaildetail.payInfoCode;
                    detail.reqSeqID = commonSubmitDetaildetail.reqSeqID;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSign[1];
                }
                retAts.writeDetail(detail);
            }
        } catch (Exception e) {
            RetAtsB119BillRelePledSubmitDetail detail = new RetAtsB119BillRelePledSubmitDetail();
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            retAts.writeDetail(detail);
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    private RetAtsCommonSubmitDetail getRspResultCode(String[] tmpStrRet, String reqSeqID, String bankCode) throws Exception {
        return getRspResultCode(tmpStrRet,reqSeqID,bankCode,"");
    }

    //通用解析结果
    private RetAtsCommonSubmitDetail getRspResultCode(String[] tmpStrRet, String reqSeqID, String bankCode,String transCode) throws Exception {
        RetAtsCommonSubmitDetail detail = new RetAtsCommonSubmitDetail();
        Connection conn = null;
        try {
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstTextNotNull("/HZBANKB2C/ebank/common/retCode");
                    String tmprspmsg = tmpBkReader.getFirstTextNotNull("/HZBANKB2C/ebank/common/retMsg");
                    detail.respCode = "0";
                    detail.respInfo = tmprspcod + "+" + tmprspmsg;
                    detail.reqSeqID = reqSeqID;
                    if (tmprspcod != null && isActiveRetCode(tmprspcod)) {
                        if("PY4011".equals(transCode)){//出票才需要记录billID
                            String billId = tmpBkReader.getFirstTextNotNull("/HZBANKB2C/ebank/output/billId");//票据ID
                            if(StringHelper.hasAnyChar(billId)){
                                conn = FGBiz.getLocalDbConn();
                                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{reqSeqID, bankCode});
                                if (selMap.size() == 0) {
                                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                                    JDBCHelper.update(conn, INSERTSEQ, new Object[]{uuid, bankCode, Format.Date8Format.format(new Date()), reqSeqID, billId});
                                }
                            }else{
                                WriteBankLogLn("出票申请未返回票据ID.");
                            }
                        }
                        if (StringHelper.hasAnyChar(tmpBkReader.getFirstTextNotNull("/HZBANKB2C/ebank/output/orderSTT"))) {
                            detail.transState = tmprspcod + tmpBkReader.getFirstTextNotNull("/HZBANKB2C/ebank/output/orderSTT");
                        }
                        if (StringHelper.hasAnyChar(tmpBkReader.getFirstTextNotNull("/HZBANKB2C/ebank/output/result/row/orderState"))) {
                            detail.transState += tmpBkReader.getFirstTextNotNull("/HZBANKB2C/ebank/output/result/row/orderState");
                        }
                        if (StringHelper.hasAnyChar(tmpBkReader.getFirstTextNotNull("/HZBANKB2C/ebank/output/result/row/errorMessage"))) {
                            detail.respInfo = tmpBkReader.getFirstTextNotNull("/HZBANKB2C/ebank/output/result/row/errorMessage");
                        }
                    } else {
                        detail.transState = tmprspcod;
                    }
                    detail.payInfoCode = detail.transState;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (conn != null) {
                conn.close();
            }
        }
        return detail;
    }
}
